package com.learn.thread;

import lombok.Data;

public class TestUnSafeThreadSynchronized2 {
    public static void main(String[] args) {
        Bank userA = new Bank("中国工商银行",1000, 1000);
        Thread t1 = new Thread(userA, "小明");
        Thread t2 = new Thread(userA, "小红");
        t1.start();
        t2.start();

    }
}

@Data
class Account {
    private String account;
    private int balance;
}

class Bank implements Runnable {

    @Override
    public void run() {
        drawMoney(drawMoney);
    }

    public Bank() {
    }
    public Bank(String account, int drawMoney) {
        this.account = account;
        this.drawMoney = drawMoney;
    }
    public Bank(String account, int balance, int drawMoney) {
        this.account = account;
        this.balance = balance;
        this.drawMoney = drawMoney;
    }

    private String account;
    private int balance;
    private int drawMoney;

    public synchronized void drawMoney(int drawMoney) {
        if (balance >= drawMoney) {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
            System.out.println(Thread.currentThread().getName() + "取钱成功，取钱：" + drawMoney);
            balance -= drawMoney;
            System.out.println("余额：" + balance);
        } else {
            System.out.println(Thread.currentThread().getName() + "取钱失败，余额不足");
        }
    }


}